/**
 * @author Yuliy Khorolinskyy
 *
 * Reaktionsspiel data
 *
 * 02.01.2012
 */
package data;

import data.HighscoreEntry.DIFFICULTY;

import java.awt.SystemTray;
import java.io.File;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * @author Yuliy
 *
 */
@XmlRootElement(namespace = "http://SE.reaktionsspiel.highscores.de/", name = "Reaktionsspiel_Highscores")
public class HighScores {
	@XmlElement(name = "highscore")
	private List<HighscoreEntry> highscores = new ArrayList<HighscoreEntry>();

	@XmlAttribute
	public static final int MAX_NUMBER_OF_ENTRIES_PER_DIFFICULTY = 3;

	@XmlAttribute
	public static final int CHANCE_FOR_HIGHSCORE_MIN_SC = 5;

	private static File highscoresFile;
	
	public HighScores(){
//		Player p1 = new Player("Spieler 1", 20, 0, true);
//		this.add(p1, DIFFICULTY.EASY);
//		this.add(p1, DIFFICULTY.MEDIUM);
//		this.add(p1, DIFFICULTY.HARD);
	}

	public HighscoreEntry[] getHsAsArray() {
		HighscoreEntry[] hsArray = new HighscoreEntry[0];
		return highscores.toArray(hsArray);
	}

	public DefaultTableModel getHsAsSwingTableModel(DIFFICULTY difficulty){
		DefaultTableModel t = new DefaultTableModel(new String[]{"#", "Name", "Score", "Time"}, MAX_NUMBER_OF_ENTRIES_PER_DIFFICULTY);


		int rowNum = 0;
		for (HighscoreEntry hs : highscores) {
			if(hs.getDifficulty() == difficulty){
				t.setValueAt((rowNum + 1) + ".", rowNum, 0);
				t.setValueAt(hs.getPlayerName(), rowNum, 1);
				t.setValueAt(hs.getScore() + "%", rowNum, 2);
				t.setValueAt(DateFormat.getDateInstance(DateFormat.SHORT).format((hs.getDate().getTime())), rowNum, 3);

				rowNum ++;
			}
		}

		return t;
	}

	public void setHs(ArrayList<HighscoreEntry> hs) {
		this.highscores = hs;
		sortAndRemoveOverhead();
	}

	public void add(HighscoreEntry s){
		this.highscores.add(s);
		sortAndRemoveOverhead();
	}
	
	public void add(Player pl, DIFFICULTY difficulty) {
		this.highscores.add(new HighscoreEntry(pl.getName(), difficulty, pl.getPoints(), Calendar.getInstance()));

	}

	private void sortAndRemoveOverhead(){
		Collections.sort(highscores, new Comparator<HighscoreEntry>() {

			@Override
			public int compare(HighscoreEntry o1, HighscoreEntry o2) {
				return - o1.compareTo(o2);
			}
		});
		
		int[] entryNum = new int[3];
		for (int i = 0; i < highscores.size(); ++i) {
			if(entryNum[highscores.get(i).getDifficulty().ordinal()] >= MAX_NUMBER_OF_ENTRIES_PER_DIFFICULTY) highscores.remove(i);
			else entryNum[highscores.get(i).getDifficulty().ordinal()] += 1;
		}
	}

	public void deleteHighscores(DIFFICULTY difficulty) {
		
		if(difficulty == DIFFICULTY.ALL)highscores.clear();
		else{
			
			List<HighscoreEntry> toRemove  = new ArrayList<HighscoreEntry>();
			for (HighscoreEntry hs : highscores) {
				if(hs.getDifficulty() == difficulty)toRemove.add(hs);
			}
			
			highscores.removeAll(toRemove);
		}
	}

	public static File getHighscoresFile() {
		return highscoresFile;
	}

	public static void setHighscoresFile(File highscoresFile) {
		HighScores.highscoresFile = highscoresFile;
	}
}
